#! usr/bin/python
# -*- coding:utf-8 -*-
import struct
import sys, os

readsize = 1024

def join(fromdir, tofile):
    """
    将分开的文件重新合并为原文件
    :param fromdir: 分开的小文件
    :param tofile: 原文件
    :return:
    """

    partfiles = os.listdir(fromdir)
    partfiles.sort()
    with open(tofile, 'wb') as output:
        for eachpart in partfiles:
            filepath = os.path.join(fromdir, eachpart)
            with open(filepath, 'rb') as fileobj:
                while True:
                    bytes = fileobj.read(readsize)
                    if not bytes:break
                    output.write(bytes)

# 合并安装文件为.pkg文件，适用于macos
if __name__ == '__main__':
    file_name = "v1.0.1-FR-AI-mac-beta.pkg"

    fromdir = "input" # 所有的下载文件放到同一文件夹，不许有其它文件

    tofile = f"output/{file_name}.pkg"
    try:
        join(fromdir, tofile)
    except:
        print("Error during joining file")
    else:
        print("joining completed")
